# -*- coding: utf-8 -*-
# @Time : 2021/9/12 15:48
# @Author : 阿怪
# @Email : 673691277@qq.com
# @File : mail_page.py
# @Project : crm自动化测试项目实战
'''站内信页面类'''
from selenium.webdriver.common.by import By
from page.base_page import BasePage
from time import sleep
import time

today = time.strftime('%Y-%m-%d')  # 获取当前日期
from utils.util import HOST2


class MailPage(BasePage):
    '''站内信页面类'''

    #定义定位器
    url = '{}/index.php?m=message'.format(HOST2)
    # url = 'http://192.168.3.156/crm3/index.php?m=message'
    add_send_locator = (By.ID, 'send')  #【写信】按钮定位器
    choice_role = (By.NAME, 'to_role_id[]')  #选择收件人‘admin’
    text_locator = (By.NAME, 'content')  # 内容输入框定位器
    submit_locator = (By.NAME, 'submit')  # 发送按钮定位器
    gettext_locator = (By.CSS_SELECTOR,'#form1 > table > tbody > tr > td:nth-child(2) > a')  #首页首条信息内容定位器
    operation_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div > div.span12.nav > div > ul > li.nav.pull-left > div > a')  # 批量操作定位器
    delete_locator = (By.ID, 'delete_receive')  # 删除定位器
    delete_message_locator = (By.XPATH,'/html/body/div[5]/div[2]')  #删除成功提示语定位器
    check_locator = (By.CSS_SELECTOR, '#form1 > table > tbody > tr > td:nth-child(1) > input')  #选择最新消息定位器
    send_massage_loator = (By.CSS_SELECTOR,'body > div.container > div.alert.alert-success')  #发送成功提示语定位器
    checktext_locator = (By.XPATH,'/html/body/div[5]/div[2]/div/div/table/tbody/tr[3]/td[2]/pre') #查看界面内容定位器
    reply_locator = (By.ID, 'reply')   #点击【回复】按钮定位器
    reply_text_locator = (By.NAME, 'content')  #回复内容输入框定位器



    #操作元素方法
    def add_send_click(self):
        '''点击【写信】'''
        element = self.driver.find_element(*self.add_send_locator)
        element.click()

    def edit_mailtext(self,text):
        '''编辑站内信内容'''
        element = self.driver.find_element(*self.text_locator)
        element.send_keys(text)

    def edit_role(self):
        '''选择收件人admin'''
        element = self.driver.find_element(*self.choice_role)
        element.click()

    def submit_click(self):
        '''保存并发送编辑'''
        element = self.driver.find_element(*self.submit_locator)
        element.click()

    def get_mailtext(self):
        '''首页获取编辑好的邮件内容'''
        element = self.driver.find_element(*self.gettext_locator)
        text = element.text
        return text

    def get_del_message(self):
        '''获取删除成功提示语'''
        element = self.driver.find_element(*self.delete_message_locator)
        text = element.text
        return text

    def click_newest(self):
        '''点击最新一条选择'''
        element = self.driver.find_element(*self.check_locator)
        element.click()

    def click_operation(self):
        '''点击批量操作'''
        element1 = self.driver.find_element(*self.operation_locator)
        element1.click()  # 点击批量操作

    def click_delete(self):
        '''点击删除'''
        element2 = self.driver.find_element(*self.delete_locator)
        element2.click()

    def delete_mail(self):
        '''删除最新一条消息'''
        self.click_newest()
        self.click_operation()
        self.click_delete()
        self.alert()



    def get_sendoff(self):
        '''获取发送成功提示语'''
        element = self.driver.find_element(*self.send_massage_loator)
        text = element.text
        return text

    def check_mail(self):
        '''点击首条消息内容进行查看'''
        element = self.driver.find_element(*self.gettext_locator)
        element.click()

    def get_checktext(self):
        '''获取查看页面的短信内容'''
        element = self.driver.find_element(*self.checktext_locator)
        text = element.text
        return text

    def click_reply(self):
        '''点击回复'''
        element = self.driver.find_element(*self.reply_locator)
        element.click()

    def input_text(self, text):
        '''输入回复内容'''
        element1 = self.driver.find_element(*self.reply_text_locator)
        element1.send_keys(text)

    def reply_text(self,text):
        '''回复站内信'''
        self.click_reply()
        sleep(1)
        self.input_text(text)




